#include "textFinder.h"

TextFinder::TextFinder(QWidget *parent) : QWidget(parent) {
    // Component
    this->findTextLabel = new QLabel("Find:   ", this);
    this->findTextInput = new QLineEdit(this);
    this->replacerLabel = new QLabel("Replace:", this);
    this->replacerInput = new QLineEdit(this);
    this->wholeWordLabel = new QLabel("Whole Word");
    this->wholeWordCheckBox = new QCheckBox(this); this->wholeWordCheckBox->setShortcut(tr("Ctrl+Shift+W"));
    this->caseSensitiveLabel = new QLabel("Case Sensitive");
    this->caseSensitiveCheckBox = new QCheckBox(this); this->caseSensitiveCheckBox->setShortcut(tr("Ctrl+Shift+C"));
    this->findNextButton = new QPushButton(this); this->findNextButton->setText("Find Next");
    this->findNextButton->setShortcut(Qt::Key_Return);
    this->findPrevButton = new QPushButton(this); this->findPrevButton->setText("Find Previous");
	this->findPrevButton->setShortcut(tr("Ctrl+Shift+P"));
    this->replaceButton = new QPushButton(this); this->replaceButton->setText("Replace");
	this->replaceButton->setShortcut(tr("Ctrl+Shift+R"));
    this->replaceAllButton = new QPushButton(this); this->replaceAllButton->setText("Replace All");
    this->replaceAllButton->setShortcut(tr("Ctrl+Shift+T"));

    this->findTextWidget = new QWidget(this);
    this->replacerWidget = new QWidget(this);
    this->modeWidget = new QWidget(this);
    this->bottomWidget = new QWidget(this);

    // Layout
    this->findTextLayout = new QHBoxLayout();
    this->replacerLayout = new QHBoxLayout();
    this->modeLayout = new QHBoxLayout();
    this->bottomLayout = new QHBoxLayout();
    this->mainLayout = new QVBoxLayout();

    this->findTextLayout->addWidget(this->findTextLabel);
    this->findTextLayout->addWidget(this->findTextInput);
    this->replacerLayout->addWidget(this->replacerLabel);
    this->replacerLayout->addWidget(this->replacerInput);
    this->modeLayout->addWidget(this->wholeWordLabel);
    this->modeLayout->addWidget(this->wholeWordCheckBox);
    this->modeLayout->addWidget(this->caseSensitiveLabel);
    this->modeLayout->addWidget(this->caseSensitiveCheckBox);
    this->bottomLayout->addWidget(this->findNextButton);
    this->bottomLayout->addWidget(this->findPrevButton);
    this->bottomLayout->addWidget(this->replaceButton);
    this->bottomLayout->addWidget(this->replaceAllButton);

    this->findTextWidget->setLayout(this->findTextLayout);
    this->replacerWidget->setLayout(this->replacerLayout);
    this->modeWidget->setLayout(this->modeLayout);
    this->bottomWidget->setLayout(this->bottomLayout);

    this->mainLayout->addWidget(this->findTextWidget);
    this->mainLayout->addWidget(this->replacerWidget);
    this->mainLayout->addWidget(this->modeWidget);
    this->mainLayout->addWidget(this->bottomWidget);
    this->setLayout(this->mainLayout);

    // Final
    this->setWindowIcon(QIcon(":/Images/Celestia.ico"));
    this->setWindowTitle("MyEditor - TextFinder");
    this->changeItemsFont();
    this->setWindowFlags(Qt::Dialog);

    connect(this->findNextButton, &QPushButton::clicked, this, &TextFinder::slot_findNext);
    connect(this->findPrevButton, &QPushButton::clicked, this, &TextFinder::slot_findPrev);
    connect(this->replaceButton, &QPushButton::clicked, this, &TextFinder::slot_replace);
    connect(this->replaceAllButton, &QPushButton::clicked, this, &TextFinder::slot_replaceAll);
}

TextFinder::~TextFinder() {}

void TextFinder::getDefaultFontOpts(const char* fontFamily, const int& fontSize, const QFont::Weight& fontWeight, const bool& useItalic) {
    this->itemFont = QFont(fontFamily, fontSize, fontWeight, useItalic);
    this->changeItemsFont();
}

void TextFinder::changeItemsFont() {
    this->findTextLabel->setFont(this->itemFont);
    this->findTextInput->setFont(this->itemFont);
    this->replacerLabel->setFont(this->itemFont);
    this->replacerInput->setFont(this->itemFont);
    this->wholeWordLabel->setFont(this->itemFont);
    this->wholeWordCheckBox->setFont(this->itemFont);
    this->caseSensitiveLabel->setFont(this->itemFont);
    this->caseSensitiveCheckBox->setFont(this->itemFont);
    this->findNextButton->setFont(this->itemFont);
    this->findPrevButton->setFont(this->itemFont);
    this->replaceButton->setFont(this->itemFont);
    this->replaceAllButton->setFont(this->itemFont);
}

void TextFinder::slot_findNext() {
    emit this->signalFindNext(this->findTextInput->text(), this->wholeWordCheckBox->isChecked(), this->caseSensitiveCheckBox->isChecked());
}

void TextFinder::slot_findPrev() {
    emit this->signalFindPrev(this->findTextInput->text(), this->wholeWordCheckBox->isChecked(), this->caseSensitiveCheckBox->isChecked());
}

void TextFinder::slot_replace() {
    emit this->signalReplace(this->findTextInput->text(), this->replacerInput->text(), this->wholeWordCheckBox->isChecked(), this->caseSensitiveCheckBox->isChecked(), false);
}

void TextFinder::slot_replaceAll() {
    emit this->signalReplaceAll(this->findTextInput->text(), this->replacerInput->text(), this->wholeWordCheckBox->isChecked(), this->caseSensitiveCheckBox->isChecked());
}
